﻿namespace YidanSoft.Library.EditorUtility.General
{
    using System;
    using System.Drawing;
    using System.Reflection;
    using System.Resources;
    using System.Windows.Forms;

    public class ResourceUtil
    {
        public static Bitmap LoadBitmapResource(System.Type assemblyType, string resourceHolder, string imageName)
        {
            Assembly assembly = Assembly.GetAssembly(assemblyType);
            ResourceManager manager = new ResourceManager(resourceHolder, assembly);
            return (Bitmap) manager.GetObject(imageName);
        }

        public static Bitmap LoadBitmapStream(System.Type assemblyType, string imageName)
        {
            return LoadBitmapStream(assemblyType, imageName, false, new Point(0, 0));
        }

        public static Bitmap LoadBitmapStream(System.Type assemblyType, string imageName, Point transparentPixel)
        {
            return LoadBitmapStream(assemblyType, imageName, true, transparentPixel);
        }

        protected static Bitmap LoadBitmapStream(System.Type assemblyType, string imageName, bool makeTransparent, Point transparentPixel)
        {
            Bitmap bitmap = new Bitmap(Assembly.GetAssembly(assemblyType).GetManifestResourceStream(imageName));
            if (makeTransparent)
            {
                Color pixel = bitmap.GetPixel(transparentPixel.X, transparentPixel.Y);
                bitmap.MakeTransparent(pixel);
            }
            return bitmap;
        }

        public static Icon LoadIconResource(System.Type assemblyType, string resourceHolder, string imageName)
        {
            Assembly assembly = Assembly.GetAssembly(assemblyType);
            ResourceManager manager = new ResourceManager(resourceHolder, assembly);
            return (Icon) manager.GetObject(imageName);
        }

        public static Icon LoadIconStream(System.Type assemblyType, string iconName)
        {
            return new Icon(Assembly.GetAssembly(assemblyType).GetManifestResourceStream(iconName));
        }

        public static Icon LoadIconStream(System.Type assemblyType, string iconName, Size iconSize)
        {
            return new Icon(LoadIconStream(assemblyType, iconName), iconSize);
        }

        public static ImageList LoadImageListResource(System.Type assemblyType, string resourceHolder, string imageName, Size imageSize)
        {
            return LoadImageListResource(assemblyType, resourceHolder, imageName, imageSize, false, new Point(0, 0));
        }

        public static ImageList LoadImageListResource(System.Type assemblyType, string resourceHolder, string imageName, Size imageSize, bool makeTransparent, Point transparentPixel)
        {
            ImageList list = new ImageList();
            list.ImageSize = imageSize;
            Assembly assembly = Assembly.GetAssembly(assemblyType);
            ResourceManager manager = new ResourceManager(resourceHolder, assembly);
            Bitmap bitmap = (Bitmap) manager.GetObject(imageName);
            if (makeTransparent)
            {
                Color pixel = bitmap.GetPixel(transparentPixel.X, transparentPixel.Y);
                bitmap.MakeTransparent(pixel);
            }
            list.Images.AddStrip(bitmap);
            return list;
        }

        public static ImageList LoadImageListStream(System.Type assemblyType, string imageName, Size imageSize)
        {
            return LoadImageListStream(assemblyType, imageName, imageSize, false, new Point(0, 0));
        }

        public static ImageList LoadImageListStream(System.Type assemblyType, string imageName, Size imageSize, Point transparentPixel)
        {
            return LoadImageListStream(assemblyType, imageName, imageSize, true, transparentPixel);
        }

        public static ImageList LoadImageListStream(System.Type assemblyType, string imageName, Size imageSize, bool makeTransparent, Point transparentPixel)
        {
            ImageList list = new ImageList();
            list.ImageSize = imageSize;
            Bitmap bitmap = new Bitmap(Assembly.GetAssembly(assemblyType).GetManifestResourceStream(imageName));
            if (makeTransparent)
            {
                Color pixel = bitmap.GetPixel(transparentPixel.X, transparentPixel.Y);
                bitmap.MakeTransparent(pixel);
            }
            list.Images.AddStrip(bitmap);
            return list;
        }
    }
}

